package com.lovo.bbs.bo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import javax.naming.NamingException;

import com.lovo.bbs.dao.PostDao;
import com.lovo.bbs.po.PostPo;
import com.lovo.bbs.vo.PostVo;

/**
 * 回帖Bo
 * @author tiancen2001
 *
 */
public class PostBo {
	 //操作类型
	 public static final int OPER_OF_UPDATE=2;
	 public static final int OPER_OF_DELETE=3;

	/**
	 * 取得指定ID的主题的所有回复Vo
	 * @param topicid
	 * @return
	 */
	public  ArrayList<PostVo> getAllPost(int topicid) {
		ArrayList<PostVo> vos = new ArrayList<PostVo>();
		ArrayList<PostPo> pos=null;
		try {
			pos = new PostDao().getAllPost(topicid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Iterator<PostPo> it = pos.iterator();
		while(it.hasNext()){
			PostPo po = it.next();
			PostVo vo = new PostVo();
			
			vo.setContent(po.getContent());
			vo.setHeadimg(po.getHeadimg());
			vo.setPostdate(po.getPostdate());
			vo.setPostid(po.getPostid());
			
			vo.setPosttitle(po.getPosttitle());
			vo.setRankid(po.getRankid());
			vo.setScore(po.getScore());
			vo.setSignature(po.getSignature());
			
			vo.setTopicid(po.getTopicid());
			vo.setUserid(po.getUserid());
			vo.setUsername(po.getUsername());
			
			vo.setUserTopicNum(po.getUserTopicNum());
			
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 新增一个回帖
	 * @param postvo
	 * @return
	 */
	public  int addOnePost(PostVo postvo) {
		int postid=0;
		PostPo po =  new PostPo();
		po.setTopicid(postvo.getTopicid());
		po.setPosttitle(postvo.getPosttitle());
		po.setContent(postvo.getContent());
		po.setUsername(postvo.getUsername());
	    try {
			postid = new PostDao().addOnePost(po);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return postid;
	}
  
	/**
	 * 取得指定ID的论坛的所有回帖
	 * @param foumid
	 * @return
	 */
	public  ArrayList<PostVo> getPostByForumID(int forumid,int queryPage){
		ArrayList<PostVo> vos = new ArrayList<PostVo>();
		ArrayList<PostPo> pos=null;
		try {
			pos = new PostDao().getPostByForumID(forumid,queryPage);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Iterator<PostPo> it = pos.iterator();
		while(it.hasNext()){
			PostPo po = it.next();
			PostVo vo = new PostVo();
			
			vo.setContent(po.getContent());
			vo.setHeadimg(po.getHeadimg());
			vo.setPostdate(po.getPostdate());
			vo.setPostid(po.getPostid());
			
			vo.setPosttitle(po.getPosttitle());
			vo.setRankid(po.getRankid());
			vo.setScore(po.getScore());
			vo.setSignature(po.getSignature());
			
			vo.setTopicid(po.getTopicid());
			vo.setUserid(po.getUserid());
			vo.setUsername(po.getUsername());
			
			vo.setUserTopicNum(po.getUserTopicNum());
			
			vo.setTopicTitle(po.getTopicTitle());
			
			vos.add(vo);
		}
		return vos;
	}
	
	/**
	 * 取得指定ID的论坛的回帖数
	 * @param foumid
	 * @return
	 */
	public  int getPostNumByForumID(int forumid){
		int postNum = 0;
		try {
			postNum = new PostDao().getPostNumByForumID(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {

			  e.printStackTrace();
		}
		return postNum;
		
	}
	
	/**
	 * 取得指定ID回帖
	 */
	public  PostVo getPostByID(int postid){
		PostVo vo = new PostVo();
		PostPo po = null;
		try {
			po = new PostDao().getPostByID(postid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		vo.setContent(po.getContent());
		vo.setHeadimg(po.getHeadimg());
		vo.setPostdate(po.getPostdate());
		vo.setPostid(po.getPostid());
		
		vo.setPosttitle(po.getPosttitle());
		vo.setRankid(po.getRankid());
		vo.setScore(po.getScore());
		vo.setSignature(po.getSignature());
		
		vo.setTopicid(po.getTopicid());
		vo.setUserid(po.getUserid());
		vo.setUsername(po.getUsername());
		
		vo.setUserTopicNum(po.getUserTopicNum());
		
		vo.setTopicTitle(po.getTopicTitle());
		
		return vo;
	}
	
	/**
	 * 回帖管理控制更新
	 */
	public  int updatePost(PostVo vo,int OperType){
		int updated = 0;
		
		PostPo po = new PostPo();

		po.setPostid(vo.getPostid());
		po.setPosttitle(vo.getPosttitle());

		
		try {
			updated = new PostDao().updatePost(po,OperType);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return updated;
		
	}

	/**
	 * 找到该回复所在的论坛
	 * @param postid 
	 * @return
	 */
	public  int getForumIDByPostID(int postid) {
		int forumid = 0;
		try {
			forumid = new PostDao().getForumIDByPostID(postid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return forumid;
	}
	/**
	 * 删除该用户的回帖,返回回帖所属的主题
	 */
	public  HashSet<Integer> delPostByUserID(int userid){
		HashSet<Integer> set = new HashSet<Integer>();
		try {
			set = new PostDao().delPostByUserID(userid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return set;
	}
}
